package mvp.demo.V.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import mvp.demo.R;

/**
 * Created by cjz on 2018/7/25.
 */
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.VH> {

    private List<String> items;

    private View.OnClickListener l;

    public void init(List<String> items, View.OnClickListener l) {
        this.items = items;
        this.l = l;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
        return new MainAdapter.VH(convertView);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {

        holder.textView.setText(items.get(position));
        holder.textView.setOnClickListener(l);
    }

    @Override
    public int getItemCount() {
        if (items == null) {
            return 0;
        }
        return items.size();
    }

    static class VH extends RecyclerView.ViewHolder {
        TextView textView;

        VH(View itemView) {
            super(itemView);
            this.textView = itemView.findViewById(R.id.textView);
        }
    }
}
